﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.Alarm
{
    class alarmDetect
    {
        public static System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(Application.StartupPath + "\\sound\\ALARM3.wav");  


        public Thread detectAlarm;
        public static Thread detectAlarmProtect = new Thread(ThreadDetectAlarmProtect);

        public static bool b_check { get; set; }

        private static alarmDetect _instance;
        private bool started = false;
        private alarmDetect() 
        {


        }
        
        public void playAlarmSound()
        {
            sndPlayer.PlayLooping();
        }

        public void stopAlarmSound()
        {
            sndPlayer.Stop();
        }

        public static alarmDetect instance
        {
            get
            {
                if (_instance == null)
                    _instance = new alarmDetect();
                return _instance;
            }
        }

        public alarm_type DetermineAlarmType(sensor_type sensor, double value, double threshold, double threshold_two, double threshold_three)
        {   
            alarm_type rc = alarm_type.normal;
            switch (sensor)
            {
                case sensor_type.tempture:

                case sensor_type.humidity:

                case sensor_type.shake:
      
                    if (threshold <= value && value < threshold_two)
                    {
                        rc = alarm_type.mini;
                    }
                    else if (threshold_two <= value && value < threshold_three)
                    {
                        rc = alarm_type.med;
                    }
                    else if (threshold_three <= value)
                    {
                        rc = alarm_type.high;
                    }
                    break;

                case sensor_type.voltage:
                    
                    if (threshold >= value && value > threshold_two)
                    {
                        rc = alarm_type.mini;
                    }
                    else if (threshold_two >= value && value > threshold_three)
                    {
                        rc = alarm_type.med;
                    }
                    else if (threshold_three >= value)
                    {
                        rc = alarm_type.high;
                    }
                    break;

                default:
                    break;
            }
            

            return rc;
        }

        public bool Alarm_Report(sensor_type sensor, alarm_type alarmT, int monitorDev_Id, int device_Id,
                                 string deviceId_Name, string zone,
                                 double value, double threshold, double threshold_two, double threshold_three)
        {
            bool rc = false;
            try
            {
                tb_alarm alarm_new = new tb_alarm();

                alarm_new.MonitorDevice_Id = monitorDev_Id;
                alarm_new.Device_Id = device_Id;
                alarm_new.Name = deviceId_Name;
                alarm_new.Alarm_Time = DateTime.Now;
                alarm_new.Value = value;
                alarm_new.Threshold = threshold;
                alarm_new.Threshold_Two = threshold_two;
                alarm_new.Threshold_Three = threshold_three;
                alarm_new.Note = "阀值更改触发告警";
                alarm_new.Checked = false;

                switch (sensor)
                {
                    case sensor_type.tempture:
                        alarm_new.Sensor = "温度";
                        break;

                    case sensor_type.humidity:
                        alarm_new.Sensor = "湿度";
                        break;

                    case sensor_type.shake:
                        alarm_new.Sensor = "震动";
                        break;
                    case sensor_type.voltage:
                        alarm_new.Sensor = "电压";
                        break;
                    default:
                        break;
                }

                switch (alarmT)
                {
                    case alarm_type.high:
                        alarm_new.Alarm_Type = "高";
                        break;

                    case alarm_type.med:
                        alarm_new.Alarm_Type = "中";
                        break;

                    case alarm_type.mini:
                        alarm_new.Alarm_Type = "低";
                        break;

                    default:
                        break;
                }

                Monitor_DB.instance.db.tb_alarm.InsertOnSubmit(alarm_new);
                Monitor_DB.instance.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                rc = true;
            }
            catch (Exception ex)
            {
                Monitor_DB.instance.Refresh_DB();
                MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);

                rc = false;
            }

            
            return rc;
        }

        public void Release_thread()
        {
            if (started)
            {
                started = false;
                b_check = false;

                if (detectAlarm.ThreadState == ThreadState.Running)
                {
                    detectAlarm.Abort();
                    //detectAlarm.Join();
                }

                if (detectAlarmProtect.ThreadState == ThreadState.Running)
                {
                    detectAlarmProtect.Abort();
                    //detectAlarmProtect.Join();
                }
            }
        }

        public void detect_alarm()
        {
            try
            {
                if (!started)
                {
                    detectAlarm = new Thread(ThreadDetectAlarm);
                    b_check = true;
                    detectAlarm.Start();
                    started = true;
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        static void ThreadDetectAlarmProtect()
        {
            try
            {
                if (b_check)
                {
                    Alarm.alarmDetect.instance.detectAlarm.Abort();
                    Alarm.alarmDetect.instance.detectAlarm.Join();

                    if (Alarm.alarmDetect.instance.detectAlarm.ThreadState != ThreadState.Running)
                    {
                        Alarm.alarmDetect.instance.detectAlarm = new Thread(Alarm.alarmDetect.instance.ThreadDetectAlarm);
                        Alarm.alarmDetect.instance.detectAlarm.Start();
                    }
                }
            }
            catch (Exception Ex)
            {
                //MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void ThreadDetectAlarm()
        {
            try
            {
                if (detectAlarmProtect.ThreadState == ThreadState.Running)
                {
                    detectAlarmProtect.Abort();
                    detectAlarmProtect.Join();
                }

                while (b_check)
                {
                    // 获取当前Alarm
                    IEnumerable<tb_alarm> qAlarm =
                    from alarm in Monitor_DB.instance.db.tb_alarm
                    where alarm.Checked != true
                    select alarm;

                    foreach (tb_alarm item in qAlarm)
                    {
                        switch (item.Alarm_Type)
                        {
                            case "高":
                                //alarmSound.Ctlcontrols.play();
 
                                FormAlarmDetect h = new FormAlarmDetect(item);
                                h.ShowDialog();
                                break;
                            case "中":
                                FormAlarmDetect m = new FormAlarmDetect(item);
                                m.ShowDialog();
                                break;
                            case "低":
                                FormAlarmDetect l = new FormAlarmDetect(item);
                                l.ShowDialog();
                                break;

                            default:
                                break;
                        }
                    }
                   
                    // wait 8 sec
                    Thread.Sleep(8000);

                    
                }
            }
            catch (Exception Ex)
            {
                //MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (b_check)
                {
                    if (detectAlarmProtect.ThreadState == ThreadState.Running)
                    {
                        detectAlarmProtect.Abort();
                        detectAlarmProtect.Join();
                    }

                    if (detectAlarmProtect.ThreadState != ThreadState.Running)
                    {
                        detectAlarmProtect = new Thread(ThreadDetectAlarmProtect);
                        detectAlarmProtect.Start();
                    }
                }
                
            }
        }

        public void show_High()
        {
            // 获取当前Alarm
            IEnumerable<tb_alarm> qAlarm =
            from alarm in Monitor_DB.instance.db.tb_alarm
            where alarm.Alarm_Type == "高"
            where alarm.Checked != true
            select alarm;

            foreach (tb_alarm item in qAlarm)
            {
                 FormAlarmDetect h = new FormAlarmDetect(item);
                 h.ShowDialog();
            }
        }

        public void show_Mid()
        {
            // 获取当前Alarm
            IEnumerable<tb_alarm> qAlarm =
            from alarm in Monitor_DB.instance.db.tb_alarm
            where alarm.Alarm_Type == "中"
            where alarm.Checked != true
            select alarm;

            foreach (tb_alarm item in qAlarm)
            {
                FormAlarmDetect h = new FormAlarmDetect(item);
                h.ShowDialog();
            }
        }

        public void show_Low()
        {
            // 获取当前Alarm
            IEnumerable<tb_alarm> qAlarm =
            from alarm in Monitor_DB.instance.db.tb_alarm
            where alarm.Alarm_Type == "低"
            where alarm.Checked != true
            select alarm;

            foreach (tb_alarm item in qAlarm)
            {
                FormAlarmDetect h = new FormAlarmDetect(item);
                h.ShowDialog();
            }
        }
    }
}
